<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
class Sele extends Controller
{
    public function tabData(Request $request)
    {
      $res = $request::instance();
      $getData = $request->param();

      $start = ($getData['page']-1)* $getData['limit'];
      $list = Db::table($getData['t'])->select();
      $total = count($list);
      $list = Db::name($getData['t'])->limit($start,$getData['limit'])->select();

        $msg = [
        	  'code' => 0,
        	  'msg' => '查询成功',
            'data'=> $list,
            'count' => $total
        ];
        echo json_encode($msg);die;
    }
    public function upData(Request $request){
      $res = $request::instance();
      $getData = $request->param();

      if($getData['t'] == 'borrow'){
        if($getData['key'] == 'bid'){
          $book = Db::name('book')->where('id','=',$getData['value'])->select();
          if(empty($book)){
            $msg = [
              'code'=> 1,
              'msg'=> '书籍ID不存在'
            ];
            echo json_encode($msg);die;
          }
        }elseif( $getData['key'] == 'uid'){
          $user = Db::name('user')->where('id','=',$getData['value'])->select();
          if(empty($user)){
            $msg = [
              'code'=> 1,
              'msg'=> '用户ID不存在'
            ];
            echo json_encode($msg);die;
          }
        }
       
      }
      if($getData['t'] == 'user'){
        if($getData['key'] == 'deptid'){
          $dept = Db::name('dept')->where('id','=',$getData['value'])->select();
          if(empty($dept)){
            $msg = [
              'code'=> 1,
              'msg'=> '部门ID不存在'
            ];
            echo json_encode($msg);die;
          }
        }
      }

      $res = Db::name($getData['t'])->where('id',$getData['id'])->update([$getData['key']=>$getData['value']]);
      if($res){
        $msg = [
          'code'=> 0,
          'msg'=> '修改成功'
        ];
        echo json_encode($msg);die;
      }else{
        $msg = [
          'code'=> 1,
          'msg'=> '修改失败'
        ];
        echo json_encode($msg);die;
      }
      
    }
    public function add(Request $request){
      $res = $request::instance();
      $getData = $request->param();
      $data = $getData['data'];
      if($getData['t'] == 'borrow'){
        $book = Db::name('book')->where('id','=',$data['bid'])->select();
        // print_r($book);
        if(empty($book)){
          $msg = [
            'code'=> 1,
            'msg'=> '书籍ID不存在'
          ];
          echo json_encode($msg);die;
        }
        $user = Db::name('user')->where('id','=',$data['uid'])->select();
        if(empty($user)){
          $msg = [
            'code'=> 1,
            'msg'=> '用户ID不存在'
          ];
          echo json_encode($msg);die;
        }
      }
      if($getData['t'] == 'user'){
        $dept = Db::name('dept')->where('id','=',$data['deptid'])->select();
        if(empty($dept)){
          $msg = [
            'code'=> 1,
            'msg'=> '部门ID不存在'
          ];
          echo json_encode($msg);die;
        }
      }
      $res = Db::name($getData['t'])->insert($data);
      if($res){
        $msg = [
          'code'=> 0,
          'msg'=> '修改成功'
        ];
        echo json_encode($msg);die;
      }else{
        $msg = [
          'code'=> 1,
          'msg'=> '修改失败'
        ];
        echo json_encode($msg);die;
    }
  }

  public function dele(Request $request){
    $res = $request::instance();
    $data = $request->param();
    $dele = Db::name($data['t'])->where('id','=',$data['id'])->delete();
    if($dele){
      $msg = [
        'code'=> 0,
        'msg'=> '删除成功'
      ];
      echo json_encode($msg);die;
    }else{
      $msg = [
        'code'=> 1,
        'msg'=> '删除失败'
      ];
      echo json_encode($msg);die;
    }
  }
}
